package com.feijia.aijingjingus.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;

import com.feijia.aijingjingus.R;

import java.util.Locale;


/**
 * Created by yangshaomin on 2018/7/22.
 */

public class Dialog extends AlertDialog implements View.OnClickListener{
    Context context;
    String dialogtype;
    Intent intent ;
    ImageView iv_blue,iv_notice;

    public Dialog(@NonNull Context context,String dialogType) {
        super(context);
        this.context=context;
        this.dialogtype=dialogType;
    }

    /**
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        switch (dialogtype){
            case "bleunopen":
                setContentView(R.layout.ble_unopened_dialog_layout);
                break;
            case "bleinterrupt":
                setContentView(R.layout.ble_interrupt_dialog_layout);
                break;
            case "battery":
                setContentView(R.layout.battery_dialog_layout);
                break;
            case "notice":
                setContentView(R.layout.dialog_layout);
                break;
            case "scanok":
                setContentView(R.layout.text_dialog_layout);
                break;
            default:
                break;
        }
        initView(dialogtype);

    }

    private void initView(String dialogtype){
        switch (dialogtype){
            case "bleunopen":
                findViewById(R.id.bt_bleok).setOnClickListener(this);
                findViewById(R.id.bt_fork).setOnClickListener(this);
                iv_blue=(ImageView)findViewById(R.id.iv_blue);
                if (!isZhRCN()){
                    iv_blue.setImageResource(R.drawable.blebleunopened2);
                }else {
                    iv_blue.setImageResource(R.drawable.blebleunopened);
                }
                break;
            case "bleinterrupt":
                findViewById(R.id.bt_fork).setOnClickListener(this);
                findViewById(R.id.bt_Reconnect).setOnClickListener(this);
                break;
            case "battery":
                findViewById(R.id.bt_ok).setOnClickListener(this);
                findViewById(R.id.bt_fork).setOnClickListener(this);
                break;
            case "notice":
                findViewById(R.id.bt_open).setOnClickListener(this);
                findViewById(R.id.bt_Later).setOnClickListener(this);
                findViewById(R.id.bt_fork).setOnClickListener(this);
                iv_notice=(ImageView)findViewById(R.id.iv_notice);
                if (!isZhRCN()){
                    iv_notice.setImageResource(R.drawable.noticebg2);
                }else {
                    iv_notice.setImageResource(R.drawable.noticebg);
                }
                break;
            case "scanok":
                findViewById(R.id.bt_textok).setOnClickListener(this);
                findViewById(R.id.bt_fork).setOnClickListener(this);
                break;
            default:
                break;
        }



    }
    /**
     * 判断是否是简体中文
     */
    public static boolean isZhRCN() {
        if (Locale.getDefault().getLanguage().equalsIgnoreCase("zh")) {
            return true;
        }else {
            return false;
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_fork:
                dismiss();
                break;
            case R.id.bt_ok:
                dismiss();
                break;
            case R.id.bt_textok:
                context.startActivity(new Intent("DetectionResultActivity"));
                dismiss();
                break;
            case R.id.bt_bleok:
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_BLUETOOTH_SETTINGS);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                dismiss();

                break;
            case R.id.bt_Reconnect:
                Intent intent1 = new Intent("controlCenterActivity");
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                context.startActivity(intent1);
                dismiss();
                break;
            case R.id.bt_open:
                intent=new Intent();
                if (Build.VERSION.SDK_INT >= 26) {
                    // android 8.0引导
                    intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                    intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName());
                } else if (Build.VERSION.SDK_INT >= 21) {
                    // android 5.0-7.0
                    intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                    intent.putExtra("app_package", context.getPackageName());
                    intent.putExtra("app_uid", context.getApplicationInfo().uid);
                } else {
                    // 其他
                    intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                    intent.setData(Uri.fromParts("package", context.getPackageName(), null));
                }
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                dismiss();
                break;
            case R.id.bt_Later:
                dismiss();
                break;
            default:
                break;
        }

    }


}
